/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-06
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-02-08
 * @Description: 联系人
 */
import 'package:hive/hive.dart';

// 定义 Contact 类，用于存储联系人信息
part 'contact.g.dart';

@HiveType(typeId: 2)
class Contact {
  @HiveField(0)
  final String userId; //联系人Id
  @HiveField(1)
  final String nickname; //昵称

  @HiveField(2)
  final String username; //用户名

  @HiveField(3)
  final String avatarUrl; //头像路径

  @HiveField(4)
  final String region; //地区

  @HiveField(5)
  final String signature; //个性签名

  @HiveField(6)
  final int gender; //性别

  @HiveField(7)
  final String remark; //联系人备注

  @HiveField(8)
  final int status; //好友状态，1:好友, 2:已删除, 3:已拉黑

  @HiveField(9)
  final DateTime createdAt; //加好友时间

  Contact({
    required this.userId,
    required this.remark,
    required this.status,
    required this.nickname,
    required this.username,
    required this.avatarUrl,
    required this.region,
    required this.signature,
    required this.gender,
    required this.createdAt,
  });

  // 工厂方法，用于从 JSON 数据创建 Contact 实例
  factory Contact.fromJson(Map<String, dynamic> json) {
    return Contact(
      userId: json['contact']['_id'] ?? '',
      nickname: json['contact']['nickname'] ?? '',
      username: json['contact']['username'] ?? '',
      avatarUrl: json['contact']['avatarUrl'] ?? '',
      region: json['contact']['region'] ?? '',
      signature: json['contact']['signature'] ?? '',
      gender: json['contact']['gender'],
      remark: json['remark'] ?? '',
      status: json['status'] ?? 1,
      createdAt:
          DateTime.parse(json['createdAt'] ?? DateTime.now().toIso8601String()),
    );
  }

  // 静态方法，用于从 JSON 数据创建 Contact 列表
  static List<Contact> fromJsonList(List<dynamic> jsonList) {
    return jsonList.map((json) => Contact.fromJson(json)).toList();
  }

  static Contact fromJsonData(json) {
    return Contact.fromJson(json);
  }
}
